<?php
use core\tools\Utils;
$vars['helper']->insertMessage();
$forum = $vars['forum'];
$forumId = $forum->getId();
$topics = $vars['topics'];
$stickyTopics = $vars['stickyTopics'];
$pagination = $vars['pagination'];
$forumHeader = $vars['forumHeader'];
$userHelper = $vars['userHelper'];
$vars['helper']->addCssFile('forum_loop.css');
// to remove the container css class
$vars['helper']->setContainerClass('');

$displayDeletedLink = $vars['displayDeletedLink'];
$newTopicLink = $vars['newTopicLink'];
?>

<div id="link-back">
	<a class="button" 
		href="<?php echo $vars['helper']->createHref('Home')?>">
			<?php echo __('Home'); ?>
	</a>
	<div id="link-back-right">
		<?php if($displayDeletedLink):?>
			<?php if($vars['isDisplayDeleted']): ?>
				<a class="button" href="<?php echo $displayDeletedLink ?>"><?php echo __('Display deleted items'); ?></a>
			<?php else: ?>
				<a class="button" href="<?php echo $displayDeletedLink ?>"><?php echo __('Hide deleted items'); ?></a>
			<?php endif; ?>
			<?php ?>
		<?php endif; ?>
		<?php if( false !== $newTopicLink ):
			echo $newTopicLink;
		endif; ?>
	</div>
</div>

<?php $forumHeader->insert(); // the different link to other forums ?>
<?php if($topics): // forum loop ?>
	<div class="rounded-box">
		<?php $pagination->insert(); ?>
		<table class="forum-loop">
			<thead>
				<tr class="background_primary">
					<th id="title"><?php echo __('Topic');?></th>
					<th id="author"><?php echo __('Author');?></th>
					<th id="latest-poster"><?php echo __('Last commented by');?></th>
					<th id="postcount"><?php echo __('Posts');?></th>
					<th id="freshness"><?php echo __('Freshness');?></th>
				</tr>
			</thead>
			<tbody>
			<?php if( $stickyTopics ): 
						$size = count($stickyTopics);
						for($i = 0 ; $i < $size ; $i++):
							$topic = $stickyTopics[$i];
							$title = $topic->getTitle();
							$titleDescription = $title;
							$class = array();
							if( $i % 2 == 1 ) {
								$class[] = 'alt background_secondary' ;
							}
							if($topic->getIsDeleted()){
								$class[] = 'deleted';
								$title = '<b>[D]</b> '.$title;
							}
							if($topic->getIsSticky()){
								$class[] = 'sticky';
								$title = '<b>[S]</b> '.$title;
							}
							if(!$topic->getIsOpen()){
								$class[] = 'closed';
								$title = '<b>[C]</b> '.$title;
							}
							$class = implode(' ', $class);
					?>
						<tr<?php if( $class ){ echo ' class="'.$class.'"' ; }?>>
							<td class="title"><a class="topic-title" href="<?php $vars['helper']->insertHref('Topic', 'displaySingleTopic', array('topicId'=>$topic->getId(), 'forumId' => $forumId)); ?>"
								title="<?php echo $titleDescription; ?>"><?php echo $title; ?></a></td>
							<td class="author">
								<?php if($userHelper->setCurrentUser($topic->getPoster())):?>
									<?php echo $userHelper->insertAvatar(35); ?>
									<div class="author-name"><?php echo $userHelper->insertLinkToProfile(); ?></div>
								<?php endif ?>
							</td>
							<td class="latest-poster">
								<?php if($userHelper->setCurrentUser($topic->getLastPoster())):?>
									<?php echo $userHelper->insertAvatar(35); ?>
									<div class="lastposter-name"><?php echo $userHelper->insertLinkToProfile(); ?></div>
								<?php endif ?>
							</td>
							<td class="postcount"><?php echo $topic->getNumPosts();?></td>
							<td class="freshness"><?php echo Utils::getDataSince($topic->getTime());?></td>
						</tr>
					<?php endfor ?>
				<tr>
					<td class="tcat background_primary" colspan="99" style="height:5px"><!-- --></td>
				</tr>
			<?php endif; ?>
				<?php
					$size = count($topics);
					for($i = 0 ; $i < $size ; $i++):
						$topic = $topics[$i];
						$title = $topic->getTitle();
						$titleDescription = $title;
						$class = array();
						if( $i % 2 == 1 ) {
							$class[] = 'alt background_secondary' ;
						}
						if($topic->getIsDeleted()){
							$class[] = 'deleted';
							$title = '<b>[D]</b> '.$title;
						}
						if($topic->getIsSticky()){
							$class[] = 'sticky';
							$title = '<b>[S]</b> '.$title;
						}
						if(!$topic->getIsOpen()){
							$class[] = 'closed';
							$title = '<b>[C]</b> '.$title;
						}
						$class = implode(' ', $class);
				?>
					<tr<?php if( $class ){ echo ' class="'.$class.'"' ; }?>>
						<td class="title"><a class="topic-title" href="<?php $vars['helper']->insertHref('Topic', 'displaySingleTopic', array('topicId'=>$topic->getId(), 'forumId' => $forumId)); ?>"
							title="<?php echo $titleDescription; ?>"><?php echo $title; ?></a></td>
						<td class="author">
							<?php if($userHelper->setCurrentUser($topic->getPoster())):?>
								<?php echo $userHelper->insertAvatar(35); ?>
								<div class="author-name"><?php echo $userHelper->insertLinkToProfile(); ?></div>
							<?php endif ?>
						</td>
						<td class="latest-poster">
							<?php if($userHelper->setCurrentUser($topic->getLastPoster())):?>
								<?php echo $userHelper->insertAvatar(35); ?>
								<div class="lastposter-name"><?php echo $userHelper->insertLinkToProfile(); ?></div>
							<?php endif ?>
						</td>
						<td class="postcount"><?php echo $topic->getNumPosts();?></td>
						<td class="freshness"><?php echo Utils::getDataSince($topic->getTime());?></td>
					</tr>
				<?php endfor ?>
			</tbody>
		</table>
		<?php $pagination->insert(); ?>
	</div>
	
<?php else: // no topics in this forum ?>
	<?php $vars['helper']->addCssFile('messages.css'); ?>
	<div class="rounded-box pagged">
		<div class="message info centered"><?php echo __('No topic in this forum');?></div>
	</div>
<?php endif; ?>


<div id="bottom-button">
<?php if( false !== $newTopicLink ):
	echo $newTopicLink;
endif; ?>
</div>
